//
//  RenYuanDetailInfoVC.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/9.
//

#import "RenYuanDetailInfoVC.h"
#import "RenYuanDetailFooterView.h"
@interface RenYuanDetailInfoVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)  UITableView       *  tableView;
@property (nonatomic,strong) NSMutableArray       *  dataAry;
@property (nonatomic,strong) NSString             *  nickName;
@property (nonatomic,strong) NSString             *  roleName;
@property (nonatomic,strong) NSString             *  roleId;
@property (nonatomic,strong) NSString             *  userName;
@property (nonatomic,strong) NSString             *  inviteCode;
@property (nonatomic,strong) NSString             *  userPhone;
@property (nonatomic,strong) NSString             *  roleCode;
@property (nonatomic, strong) NSString *realName;
@property (nonatomic,strong)CommonChoseTypeRoleView *  choseTypeView;
@property (nonatomic,strong)NSMutableArray          *  jueSeArray;

@end

@implementation RenYuanDetailInfoVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
    [self layoutCustomNav:self.view title:@"人员详情"];
    if ([[NSString stringWithFormat:@"%@",self.model.roleId] isEqualToString:@"1"]){

    } else {
//        [self createRightButtonItem];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createData];
    [self getJueSeList];
    [self createTableView];
    self.view.backgroundColor = White_Color;
}

#pragma mark----初始化数据、控件
-(void)createData{
    self.jueSeArray = [NSMutableArray array];
    self.dataAry = [NSMutableArray arrayWithArray:@[@"别名",@"角色",@"真实姓名",@"手机号"]];
    self.nickName = self.model.nickName;
    self.roleName = self.model.roleName;
    self.roleId = self.model.roleId;
    self.userName = self.model.userName;
    self.userPhone = self.model.userPhone;
    self.inviteCode = self.model.inviteCode;
    self.realName = self.model.realName;
    
}

-(void)createTableView{
    self.tableView = [[UITableView  alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, ScreenWidth, self.view.height - LL_TabbarSafeBottomMargin)];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.backgroundColor = White_Color;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:[CommonTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[CommonTableViewCell reuseIdentifier]];
    [self.view addSubview:self.tableView];
}

//-(void)createRightButtonItem{
//    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth-100,self.customerTitleLabel.top, 100, 30)];
//    headerView.backgroundColor=[UIColor whiteColor];
//    [self.barView addSubview:headerView];
//
//     UIButton   *  eduitButton=[UIButton buttonWithType:UIButtonTypeCustom];
//     eduitButton.frame=CGRectMake(ScreenWidth-100,  self.customerTitleLabel.top, 80, 30);
//    [eduitButton setTitle:@"保存" forState:UIControlStateNormal];
//     eduitButton.titleLabel.font=[UIFont systemFontOfSize:FontSize(13.0)];
//     eduitButton.titleLabel.textAlignment=NSTextAlignmentLeft;
//    [eduitButton setTitleColor:White_Color forState:UIControlStateNormal];
//     eduitButton.backgroundColor = mainColor;
//    [eduitButton createBordersWithColor:nil withCornerRadius:5.0 andWidth:0];
//    [eduitButton addTarget:self action:@selector(EduitButtonClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.barView addSubview:eduitButton];
//}

-(void)EduitButtonClick{
    [self commitUpdateInfo];

}

#pragma mark---tableViewDelegate、tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataAry.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[CommonTableViewCell reuseIdentifier]];
    if (!cell) {
        cell = [[CommonTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CommonTableViewCell reuseIdentifier]];
    }
    cell.nameLb.text = self.dataAry[indexPath.row];
    cell.lineView.hidden=YES;
    cell.rightImage.hidden = YES;
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    cell.indexPath = indexPath;
    cell.userInteractionEnabled = NO;

    [cell setCurrentValueBlock:^(NSString * _Nonnull contentString, NSIndexPath * _Nonnull indexPath) {
        if (indexPath.row == 0){
            self.nickName=contentString;
       }
    }];
    switch (indexPath.row) {
        case 0:
        {
            if ([[NSString stringWithFormat:@"%@",self.model.roleId] isEqualToString:@"1"]){
                cell.contentTF.userInteractionEnabled=NO;
            } else {
                cell.contentTF.userInteractionEnabled=YES;
            }
            if (self.nickName.length == 0) {
                cell.contentTF.placeholder = @"请输入别名";
            } else {
                cell.contentTF.text = self.nickName;
            }
        }
        break;
        case 1:
        {
            cell.contentTF.userInteractionEnabled=NO;
            if (self.roleName.length == 0) {
                cell.contentTF.placeholder = @"请选择角色";
            } else {
                cell.contentTF.text = self.roleName;
            }
        }
        break;
        case 2:
        {
            cell.contentTF.userInteractionEnabled = NO;
            if (self.realName.length == 0) {
                cell.contentTF.placeholder = @"请输入真实姓名";
            } else {
                cell.contentTF.text = self.realName;
            }
        }
        break;
        case 3:
        {
            
            cell.contentTF.userInteractionEnabled=NO;
            if (self.userPhone.length == 0) {
                cell.contentTF.placeholder = @"请输入手机号";
            } else {
                cell.contentTF.text = self.userPhone;
            }
        }
        break;
        default:
        break;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [CommonTableViewCell cellHeight];
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if ([[NSString stringWithFormat:@"%@",self.model.roleId]isEqualToString:@"1"]){
        //超级管理员不能进行解绑
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
        return footerView;
    } else {
        
        UIView *footerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 150)];
        RenYuanDetailFooterView *detailView =[RenYuanDetailFooterView shareInstance];
        detailView.frame = CGRectMake(0, 0, ScreenWidth, 150);
        [footerView addSubview:detailView];
        if ([self.model.useFlag isEqualToString:@"1"]) {
            detailView.statusButton.hidden = YES;
        } else {
            detailView.statusButton.hidden = NO;
        }
        [detailView.activeButton setTitle:@"删除员工" forState:UIControlStateNormal];
        detailView.yaoQingLb.text = [NSString stringWithFormat:@"邀请码:%@",self.inviteCode];
        if ([self.roleName isEqualToString:@"超级管理员"]) {
            detailView.hidden = YES;
        }
        [detailView.fuZhiButton whenTapped:^{
            UIPasteboard *copy = [UIPasteboard generalPasteboard];
            [copy setString:self.inviteCode];
         if (copy==nil||[self.inviteCode isEqualToString:@""]){

         } else {
             showAlert(@"复制成功");
         }
        }];
        [detailView.activeButton whenTapped:^{
            UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"确定解除吗?" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                [self commitInfo];
            }]];
            [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];
            
        }];
        return footerView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if ([[NSString stringWithFormat:@"%@",self.model.roleId]isEqualToString:@"1"]){
        return 0;
    } else {
        return 150;
    }
}


#pragma mark----解绑员工、更新员工信息接口、获取角色接口
-(void)commitInfo{
     [self.view endEditing:YES];
    YLUserInfo *info = [YLUserMgr fetchUser];
//    if ([self.model.userPhone isEqualToString:info.phone]) {
//        showAlert(@"自己不能解绑自己");
//        return;
//    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:self.model.ID
               forKey:@"id"];
    [params setObject:self.model.userPhone forKey:@"unboundPhone"];
    
    
    [MSNetworkManager postRevertUrl:POST_PersonDelete_URL params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            showAlert(@"删除成功");
           [self.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
    
   
    
}


-(void)commitUpdateInfo{
    [self.view endEditing:YES];
    YLUserInfo *info = [YLUserMgr fetchUser];
    if ([self.model.userPhone isEqualToString:info.loginNo]) {
        showAlert(@"登录者不能编辑个人信息");
        return;
    }
    NSMutableDictionary  *   params =[[NSMutableDictionary alloc] init];
    if (self.nickName.length== 0) {
        showAlert(@"请输入别名");
        return;
    }
    [params setObject:self.nickName forKey:@"nickName"];
    if (self.roleName.length== 0) {
        showAlert(@"请选择角色");
        return;
    }
    [params setObject:self.roleName forKey:@"roleName"];
    [params setObject:self.roleId forKey:@"roleId"];
    if (self.userName.length== 0) {
        showAlert(@"请输入真实姓名");
        return;
    }
    [params setObject:self.userName forKey:@"userName"];
    if(self.userPhone.length== 0){
        showAlert(@"请输入手机号");
        return;
    }
    [params setObject:self.userPhone forKey:@"userPhone"];
    [params setObject:self.model.ID forKey:@"id"];
    
    
    [MSNetworkManager postRevertUrl:POST_PersonEditUser_URL params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            [self.navigationController popViewControllerAnimated:YES];
       }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
 
}

-(void)getJueSeList{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
        [params setObject:self.model.ID forKey:@"id"];
    
    [MSNetworkManager postRevertUrl:POST_PersonDetail_URL params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            NSArray  *cacheArray = resonseObj[@"roleList"];
            self.jueSeArray=[JueSeModel  mj_objectArrayWithKeyValuesArray:cacheArray];
       }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
   
}
    
    
@end
